package com.link.base.base.common.service;

import com.link.base.base.common.dao.mybatis.mapper.MsgMapper;
import com.link.base.base.common.model.Msg;
import com.link.base.base.user.model.User;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/9 10:04
 */
@Service
@Transactional(rollbackFor = ServiceException.class)
public class MsgServiceImpl implements MsgService {

    @Resource
    private MsgMapper msgMapper;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    private List<Msg> getRecord(List<Map> list) {
        List<Msg> result = new ArrayList<Msg>();

        for (Map map : list) {
            Msg msg = new Msg();
            msg.setId((Long) map.get("id"));
            msg.setTitle((String) map.get("title"));
            msg.setBeginTime((String) map.get("begin_time"));
            msg.setIsAllValid((String) map.get("is_all_valid"));
            msg.setIsPublished((String) map.get("is_published"));
            msg.setCreatedBy((Long) map.get("created_by"));
            msg.setLastUpdatedBy((Long) map.get("last_upd_by"));
            msg.setType((String) map.get("type"));
            msg.setMsgContent((String) map.get("msg_content"));

            result.add(msg);
        }
        return result;
    }

    /**
     * 添加或更新
     *
     * @param record
     * @param login
     * @throws ServiceException
     */
    @Override
    public void addOrUpdate(Msg record, User login) throws ServiceException {
        try {
            Long id = keyGenerateDao.keyGenerate();
            record.setId(id);
            msgMapper.msgInsert(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }


    }

}
